package com.erebus.service;

import org.springframework.ai.chat.client.RequestResponseAdvisor;
import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
import org.springframework.ai.chat.client.advisor.api.AdvisedResponse;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisorChain;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisorChain;
import org.springframework.ai.chat.model.ChatResponse;
import reactor.core.publisher.Flux;

import java.util.Map;


// 用于拦截请求和响应
public class LoggingAdvisor implements RequestResponseAdvisor {


    @Override
    public int getOrder() { // 返回优先级
        return 0;
    }

    @Override
    public String getName() { // 返回名称
        return RequestResponseAdvisor.super.getName();
    }

    // 请求拦截器
    @Override
    public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> adviseContext) {

        System.out.println("请求拦截");
        System.out.println("Request: " + request);
//        return RequestResponseAdvisor.super.adviseRequest(request, adviseContext);

        return request;
    }

    // 响应拦截器
    @Override
    public ChatResponse adviseResponse(ChatResponse response, Map<String, Object> adviseContext) {
        return RequestResponseAdvisor.super.adviseResponse(response, adviseContext);
    }

    // 流式响应拦截器
    @Override
    public Flux<ChatResponse> adviseResponse(Flux<ChatResponse> fluxResponse, Map<String, Object> context) {
        return RequestResponseAdvisor.super.adviseResponse(fluxResponse, context);
    }

    // 调用拦截器
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        return RequestResponseAdvisor.super.aroundCall(advisedRequest, chain);
    }

    // 流式调用拦截器
    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        return RequestResponseAdvisor.super.aroundStream(advisedRequest, chain);
    }
}
